package com.example.android.utils;

import android.content.SharedPreferences;
import android.util.Log;

import androidx.annotation.NonNull;

import java.io.IOException;

import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

public class RequestInterceptor implements Interceptor {
    private String TOKEN = "token";

    @NonNull
    @Override
    public Response intercept(Chain chain) throws IOException {
        String token = SPUtils.getString(TOKEN); //SpUtils是SharedPreferences的工具类，自行实现
        Request originalRequest = chain.request();
        if (token==null || token.isEmpty()) {
            return chain.proceed(originalRequest);
        } else {
            Request updateRequest = originalRequest.newBuilder().header("Authorization", token).build();
            Log.v("zcb", "request:" + originalRequest.toString());
            Log.v("zcb", "request headers:" + originalRequest.headers().toString());
            return chain.proceed(updateRequest);
        }
    }
}
